import ConfirmBox from './ConfirmBox.vue'
import Vue from 'vue'

export const createConfirm = (opt) => {
  const div = document.createElement('div')
  document.body.appendChild(div)

  const App = Vue.extend(ConfirmBox)
  const app = new App({
    propsData: {
      ...opt
    }
  })
  app.$on('close', () => {
    close()
    opt?.cancel?.()
  })
  app.$on('ok', () => {
    close()
    opt?.ok?.()
  })
  app.$mount(div)
  const close = () => {
    app.$destroy()
    document.body.removeChild(app.$el)
  }
  return {
    close
  }
}
